<?php
/*
###########################################################################
#                                  KALMAR                                 #
#                      http://akkar.sourceforge.net/                      #
#                                                                         #
#                            -------------------                          #
#                                upgrade.php                              #
#                            -------------------                          #
#                                                                         #
#   copyright (C) 2000-2006 Roy W. Andersen                               #
#   email: ensnared@gmail.com                                             #
#                                                                         #
###########################################################################

###########################################################################
#                                                                         #
#  This program is free software; you can redistribute it and/or modify   #
#  it under the terms of the GNU General Public License as published by   #
#  the Free Software Foundation; either version 2 of the License, or      #
#  (at your option) any later version.                                    #
#                                                                         #
###########################################################################
*/
define('IN_KALMAR', true);

# Set the error-reporting to ignore notices. This is default on *nix installations of PHP, but apparently not in Windows...
error_reporting(E_ALL ^ E_NOTICE);

if (!file_exists('conf/baseconf.php')) {
	# No existing configfile found, so this isn't an upgrade. Let's try an install instead.
	header('Location: install.php');
	exit();
}

session_start();

# Provide the config file if AKKAR was unable to create it and have the user download it
if ($_REQUEST['get_config']) {
	header('Content-type: text/plain');
	header('Content-disposition: attachment; filename=baseconf.php');
	echo $_SESSION['newconf'];
	exit();
}

$ds = DIRECTORY_SEPARATOR;
$ps = PATH_SEPARATOR;
ini_set("include_path", ".".$ps.".".$ds."conf".$ds."".$ps.".".$ds."templates".$ds."".$ps.".".$ds."scripts".$ds."".$ps.".".$ds."hjelp".$ds."".$ps . ini_get("include_path"));
include_once('baseconf.php');
@$connection = mysql_pconnect($sqlserver, $sqluser, $sqlpasswd) or exit(mysql_error());
mysql_select_db($sqlbase);
include_once('functions.php');
include_once('mysql_functions.php');
$config = get_configuration();

# Version we're upgrading to
$kalmar_version = '2.6.0_devel';

echo '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html40/loose.dtd">
<html>
<head>
	<title>KALMAR Upgrade</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
';
if ((browsertype() == 'ie') && (is_file(AKKARDIR.'/styles/'.$config['style'].'/iestyle.css'))) {
	echo '<link rel="StyleSheet" href="'.AKKARURL.'/styles/'.$config['style'].'/iestyle.css" type="text/css">';
} elseif ((browsertype() == 'opera') && (is_file(AKKARDIR.'/styles/'.$config['style'].'/operastyle.css'))) {
	echo '<link rel="StyleSheet" href="'.AKKARURL.'/styles/'.$config['style'].'/operastyle.css" type="text/css">';
} else {
	echo '<link rel="StyleSheet" href="'.AKKARURL.'/styles/'.$config['style'].'/style.css" type="text/css">';
}

if (is_file(AKKARDIR.'/styles/'.$config['style'].'/logo.png')) {
	$logo = AKKARURL.'styles/'.$config['style'].'/logo.png';
} elseif (is_file(AKKARDIR.'/styles/'.$config['style'].'/logo.jpg')) {
	$logo = AKKARURL.'styles/'.$config['style'].'/logo.jpg';
} elseif (is_file(AKKARDIR.'/styles/'.$config['style'].'/logo.gif')) {
	$logo = AKKARURL.'/styles/'.$config['style'].'/logo.gif';
} else {
	$logo = AKKARURL.'styles/default/logo.png';
}
echo '
<link rel="StyleSheet" href="'.AKKARURL.'/styles/'.$config['style'].'/common.css" type="text/css">
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
	</head>
<body>
<table border="0" align="center">
	<tr>
		<td class="banner">
			<img src="'.$logo.'" alt="KALMAR">
		</td>
	</tr>
	<tr>
		<td class="maincol" style="padding-bottom: 20em;">
		<h1 style="padding-bottom: 1em;" align="center">'.$config['arrgruppenavn'].'</h1>
';

if ($config['calamar_version'] == $kalmar_version) {
	echo '
		<div align="center">
		<h3>You have to delete <em>install.php</em> and <em>upgrade.php</em> from the KALMAR directory before the system will operate.</h3>
		<br><br><button type="button" onClick="javascript:window.location=\'./\';">OK</button>
		</div>
	';
	exit();
} else {
	echo '
		<h2 align="center">KALMAR Upgrade</h2>
		<h3 align="center">'.$config['kalmar_version'].' -&gt; '.$kalmar_version.'</h3>
';
}

if (!$_GET['do_upgrade']) {
	# First load, so we'll just show a "hi, this will upgrade, click OK" screen before we do anything messy.
	echo '
		<div align="center">
		<h4>It is recomended that you back up your current installation before performing the upgrade.<br/>
		<br/>Click the button to start the upgrade.</h4>
		<br>
		<br>
		<button type="button" onClick="javascript:window.location=\'./upgrade.php?do_upgrade=yes\';">Upgrade</button>
		</td>
	';
} else {
	# Alright, let's do the upgrade
	echo '
			<br>
			<h4>Performing upgrade steps:</h4>
			<br>
	';
	
	# Figure out the version we're upgrading from and perform steps necessary
	switch ($config['kalmar_version']) {
		default:
			echo '<p>Finalising upgrade...';
			# Yep, all upgrades needs a version-number update :)
			mysql_query("UPDATE `".$table_prefix."config` SET value='".$kalmar_version."' WHERE name='kalmar_version'", $connection) or exit(mysql_error());
			echo ' <span class="green">Done</span>.</p>';
	}
	if (!isset($delete_self)) {
		# All done, and nothing has specified we can't delete install.php and upgrade.php
		$delete_self = true;
	}

	# People love to be told everything's a-ok, so let's.
	echo '
		<div align="center" style="padding-top: 3em;">
		<h3>Upgrade complete.</h3>
		<br><br>
		<h5>You MUST delete <em>upgrade.php</em> and <em>install.php</em> from the KALMAR directory before the system will operate. This script may have already done it for you if it has the proper access-rights.
		<br><br><button type="button" onClick="javascript:window.location=\'./\';">To KALMAR</button>
		</div>
		</td>
	</tr>
	<tr>
		<td align="right"><a href="http://akkar.sourceforge.net/" target="_blank"><img src="images/akkar-powered.png" alt="AKKAR Powered"></a></td>
	</tr>
</table>
</body>
</html>
';

	if ($delete_self) {
		# Delete the upgrade and/or install-scripts if Bob's our uncle
		if (is_writeable('install.php')) {
			unlink('install.php');
		}
		if (is_writeable('upgrade.php')) {
			unlink('upgrade.php');
		}
	}
}
ob_end_flush();
?>
